/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Nomenclaturalcode;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Taxonomicelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class NomenclaturalcodeJpaController {

    public NomenclaturalcodeJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

        public Nomenclaturalcode save(Nomenclaturalcode nomenclaturalcode) {
        Nomenclaturalcode aux = findNomenclaturalcode(nomenclaturalcode.getNomenclaturalcode());
        if(aux==null)
            return create(nomenclaturalcode);
        else
            return aux;
    }

    public Nomenclaturalcode create(Nomenclaturalcode nomenclaturalcode) {
        Nomenclaturalcode r = null;
        if (nomenclaturalcode.getTaxonomicelementList() == null) {
            nomenclaturalcode.setTaxonomicelementList(new ArrayList<Taxonomicelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Taxonomicelement> attachedTaxonomicelementList = new ArrayList<Taxonomicelement>();
            for (Taxonomicelement taxonomicelementListTaxonomicelementToAttach : nomenclaturalcode.getTaxonomicelementList()) {
                taxonomicelementListTaxonomicelementToAttach = em.getReference(taxonomicelementListTaxonomicelementToAttach.getClass(), taxonomicelementListTaxonomicelementToAttach.getIdtaxonomicelement());
                attachedTaxonomicelementList.add(taxonomicelementListTaxonomicelementToAttach);
            }
            nomenclaturalcode.setTaxonomicelementList(attachedTaxonomicelementList);
            r = em.merge(nomenclaturalcode);
            for (Taxonomicelement taxonomicelementListTaxonomicelement : nomenclaturalcode.getTaxonomicelementList()) {
                Nomenclaturalcode oldNomenclaturalcodeOfTaxonomicelementListTaxonomicelement = taxonomicelementListTaxonomicelement.getNomenclaturalcode();
                taxonomicelementListTaxonomicelement.setNomenclaturalcode(nomenclaturalcode);
                taxonomicelementListTaxonomicelement = em.merge(taxonomicelementListTaxonomicelement);
                if (oldNomenclaturalcodeOfTaxonomicelementListTaxonomicelement != null) {
                    oldNomenclaturalcodeOfTaxonomicelementListTaxonomicelement.getTaxonomicelementList().remove(taxonomicelementListTaxonomicelement);
                    oldNomenclaturalcodeOfTaxonomicelementListTaxonomicelement = em.merge(oldNomenclaturalcodeOfTaxonomicelementListTaxonomicelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Nomenclaturalcode nomenclaturalcode) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nomenclaturalcode persistentNomenclaturalcode = em.find(Nomenclaturalcode.class, nomenclaturalcode.getIdnomenclaturalcode());
            List<Taxonomicelement> taxonomicelementListOld = persistentNomenclaturalcode.getTaxonomicelementList();
            List<Taxonomicelement> taxonomicelementListNew = nomenclaturalcode.getTaxonomicelementList();
            List<Taxonomicelement> attachedTaxonomicelementListNew = new ArrayList<Taxonomicelement>();
            for (Taxonomicelement taxonomicelementListNewTaxonomicelementToAttach : taxonomicelementListNew) {
                taxonomicelementListNewTaxonomicelementToAttach = em.getReference(taxonomicelementListNewTaxonomicelementToAttach.getClass(), taxonomicelementListNewTaxonomicelementToAttach.getIdtaxonomicelement());
                attachedTaxonomicelementListNew.add(taxonomicelementListNewTaxonomicelementToAttach);
            }
            taxonomicelementListNew = attachedTaxonomicelementListNew;
            nomenclaturalcode.setTaxonomicelementList(taxonomicelementListNew);
            nomenclaturalcode = em.merge(nomenclaturalcode);
            for (Taxonomicelement taxonomicelementListOldTaxonomicelement : taxonomicelementListOld) {
                if (!taxonomicelementListNew.contains(taxonomicelementListOldTaxonomicelement)) {
                    taxonomicelementListOldTaxonomicelement.setNomenclaturalcode(null);
                    taxonomicelementListOldTaxonomicelement = em.merge(taxonomicelementListOldTaxonomicelement);
                }
            }
            for (Taxonomicelement taxonomicelementListNewTaxonomicelement : taxonomicelementListNew) {
                if (!taxonomicelementListOld.contains(taxonomicelementListNewTaxonomicelement)) {
                    Nomenclaturalcode oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement = taxonomicelementListNewTaxonomicelement.getNomenclaturalcode();
                    taxonomicelementListNewTaxonomicelement.setNomenclaturalcode(nomenclaturalcode);
                    taxonomicelementListNewTaxonomicelement = em.merge(taxonomicelementListNewTaxonomicelement);
                    if (oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement != null && !oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement.equals(nomenclaturalcode)) {
                        oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement.getTaxonomicelementList().remove(taxonomicelementListNewTaxonomicelement);
                        oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement = em.merge(oldNomenclaturalcodeOfTaxonomicelementListNewTaxonomicelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = nomenclaturalcode.getIdnomenclaturalcode();
                if (findNomenclaturalcode(id) == null) {
                    throw new NonexistentEntityException("The nomenclaturalcode with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nomenclaturalcode nomenclaturalcode;
            try {
                nomenclaturalcode = em.getReference(Nomenclaturalcode.class, id);
                nomenclaturalcode.getIdnomenclaturalcode();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The nomenclaturalcode with id " + id + " no longer exists.", enfe);
            }
            List<Taxonomicelement> taxonomicelementList = nomenclaturalcode.getTaxonomicelementList();
            for (Taxonomicelement taxonomicelementListTaxonomicelement : taxonomicelementList) {
                taxonomicelementListTaxonomicelement.setNomenclaturalcode(null);
                taxonomicelementListTaxonomicelement = em.merge(taxonomicelementListTaxonomicelement);
            }
            em.remove(nomenclaturalcode);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Nomenclaturalcode> findNomenclaturalcodeEntities() {
        return findNomenclaturalcodeEntities(true, -1, -1);
    }

    public List<Nomenclaturalcode> findNomenclaturalcodeEntities(int maxResults, int firstResult) {
        return findNomenclaturalcodeEntities(false, maxResults, firstResult);
    }

    private List<Nomenclaturalcode> findNomenclaturalcodeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Nomenclaturalcode.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public Nomenclaturalcode findNomenclaturalcode(String q) {
        Query query= getEntityManager().createNamedQuery("Nomenclaturalcode.findByNomenclaturalcode");
        query.setParameter("nomenclaturalcode",q);

        return query.getResultList().isEmpty()?null:(Nomenclaturalcode)query.getResultList().get(0);
    }
    public Nomenclaturalcode findNomenclaturalcode(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Nomenclaturalcode.class, id);
        } finally {
            em.close();
        }
    }

    public int getNomenclaturalcodeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Nomenclaturalcode> rt = cq.from(Nomenclaturalcode.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
